**********************
**Note to user: replace "your file location" in the  lines below with the location of your replication package folder
******************
global base "[your file location]\"
global output "your file location]\output_files"

cd  "${base}\output_files"
********
** PARENTING PRACTICES--POOLED SAMPLE ***
*********

estimates clear

use "${base}\replication_file\Gallup_Family_Adolescent_Health_2023_Replication_File.dta", clear
global pstyle "B11C B11F B10_1 B10_2 B10_3 B10_4 B10_5 B10_6 B10_7 B10_9 B10_10 B10_11 B10_12 B10_13 B10_14 B10_15 B10_16 B10_17 B10_18 B10_19 B10_21"
factor $pstyle  [aw=weight], mineigen(1)
rotate, oblique oblimin(0)
matrix list e(L)
esttab e(L)
esttab e(L) using parenting_pooled.csv, replace plain nomtitles 

********
** PARENTING PRACTICES--BLACK SAMPLE ***
*********

use "${base}\replication_file\Gallup_Family_Adolescent_Health_2023_Replication_File.dta", clear
encode RACE, gen(race)
keep if race==2

global pstyle "B11C B11F B10_1 B10_2 B10_3 B10_4 B10_5 B10_6 B10_7 B10_9 B10_10 B10_11 B10_12 B10_13 B10_14 B10_15 B10_16 B10_17 B10_18 B10_19 B10_21"
factor $pstyle  [aw=weight], mineigen(1)
rotate, oblique oblimin(0)
esttab e(L) using parenting_black.csv, replace plain nomtitles 

********
** PARENTING PRACTICES--HISPANIC SAMPLE ***
*********

use "${base}\replication_file\Gallup_Family_Adolescent_Health_2023_Replication_File.dta", clear
encode RACE, gen(race)
keep if race==3

global pstyle "B11C B11F B10_1 B10_2 B10_3 B10_4 B10_5 B10_6 B10_7 B10_9 B10_10 B10_11 B10_12 B10_13 B10_14 B10_15 B10_16 B10_17 B10_18 B10_19 B10_21"
factor $pstyle  [aw=weight], mineigen(1)
rotate, oblique oblimin(0)
esttab e(L) using parenting_hispanic.csv, replace plain nomtitles 


********
** PARENTING PRACTICES--WHITE SAMPLE ***
*********

use "${base}\replication_file\Gallup_Family_Adolescent_Health_2023_Replication_File.dta", clear
encode RACE, gen(race)
keep if race==5

global pstyle "B11C B11F B10_1 B10_2 B10_3 B10_4 B10_5 B10_6 B10_7 B10_9 B10_10 B10_11 B10_12 B10_13 B10_14 B10_15 B10_16 B10_17 B10_18 B10_19 B10_21"
factor $pstyle  [aw=weight], mineigen(1)
rotate, oblique oblimin(0)
esttab e(L) using parenting_white.csv, replace plain nomtitles 

********
** RELATIONSHIP QUALITY & ADVERSE EXPERIENCES--POOLED SAMPLE ***
*********
use "${base}\replication_file\Gallup_Family_Adolescent_Health_2023_Replication_File.dta", clear

global relate_item "YC20_alcohol	YC21_angry	YC23_left	YC23a_rejection	C24_safety	B11A	B11B	C19_parent_love	B6_overall_parent_ch B9_relationship_othe B10_8"
factor $relate_item [aw=weight], mineigen(1)
rotate, oblique oblimin(0)
esttab e(L) using relationship_pooled.csv, replace plain nomtitles 

/**with reverse coding
global relate_item "NC20_alcohol	NC21_angry	NC23_left	NC23a_rejection	C24_safety	REB11A	B11B	C19_parent_love	B6_overall_parent_ch B9_relationship_othe REB10_8"
factor $relate_item [aw=weight], mineigen(1)
rotate, oblique oblimin(0)
predict f1 f2 
*/

********
** RELATIONSHIP QUALITY & ADVERSE EXPERIENCES--BLACK SAMPLE ***
*********

use "${base}\replication_file\Gallup_Family_Adolescent_Health_2023_Replication_File.dta", clear
encode RACE, gen(race)
keep if race==2

global relate_item "YC20_alcohol	YC21_angry	YC23_left	YC23a_rejection	C24_safety	B11A	B11B	C19_parent_love	B6_overall_parent_ch B9_relationship_othe B10_8"
factor $relate_item [aw=weight], mineigen(1)
rotate, oblique oblimin(0)
esttab e(L) using relationship_black.csv, replace plain nomtitles 

********
** RELATIONSHIP QUALITY & ADVERSE EXPERIENCES--HISPANIC SAMPLE ***
*********

use "${base}\replication_file\Gallup_Family_Adolescent_Health_2023_Replication_File.dta", clear
encode RACE, gen(race)
keep if race==3

global relate_item "YC20_alcohol	YC21_angry	YC23_left	YC23a_rejection	C24_safety	B11A	B11B	C19_parent_love	B6_overall_parent_ch B9_relationship_othe B10_8"
factor $relate_item [aw=weight], mineigen(1)
rotate, oblique oblimin(0)
esttab e(L) using relationship_hispanic.csv, replace plain nomtitles 

********
** RELATIONSHIP QUALITY & ADVERSE EXPERIENCES--WHITE SAMPLE ***
*********

use "${base}\replication_file\Gallup_Family_Adolescent_Health_2023_Replication_File.dta", clear
encode RACE, gen(race)
keep if race==5

global relate_item "YC20_alcohol	YC21_angry	YC23_left	YC23a_rejection	C24_safety	B11A	B11B	C19_parent_love	B6_overall_parent_ch B9_relationship_othe B10_8"
factor $relate_item [aw=weight], mineigen(1)
rotate, oblique oblimin(0)
esttab e(L) using relationship_white.csv, replace plain nomtitles 


foreach x in pooled black hispanic white {
import delimited using parenting_`x'.csv, clear
gen group="`x'"
ren v1 item
gen factor="parenting"
save parenting_`x'.dta, replace
}
foreach x in pooled black hispanic white {
import delimited using relationship_`x'.csv, clear
gen group="`x'"
ren v1 item
gen factor="relationship"
save relationship_`x'.dta, replace
}


use "parenting_hispanic.dta", clear
merge 1:1 group item using "parenting_pooled.dta", nogen
merge 1:1 group item using "parenting_white.dta", nogen
merge 1:1 group item using "relationship_black.dta", nogen
merge 1:1 group item using "relationship_hispanic.dta", nogen
merge 1:1 group item using "relationship_pooled.dta", nogen
merge 1:1 group item using "relationship_white.dta", nogen
merge 1:1 group item using "parenting_black.dta", nogen

reshape wide factor1 factor2 , i(item) j(group) string
gsort factor  -factor1pooled 

ren item var
cd  "${base}\replication_file"
merge m:1 var using "label parenting items.dta", keepusing(text) keep(3) nogen
cd  "${base}\output_files"
export delimited "Exploratory_Factor_Analysis_Summary_Loadings.csv", replace
